<?php

declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\controller\system;


use app\AdminController;
use app\common\library\Ip2Region;
use app\common\model\system\User as UserModel;
use support\Response;
use system\Random;
use think\facade\Db;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;

/**
 * 用户管理
 * Class User
 * @package app\admin\controller\system
 */
class User extends AdminController
{
    // 初始化函数
    public function __construct()
    {
        parent::__construct();
        $this->model = new UserModel();
    }

    /**
     * 获取资源
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): \support\Response
    {
        if (request()->isAjax()) {
            // 获取数据
            $post = \request()->all();
            $page = (int)input('page') ?? 1;
            $limit = (int)input('limit') ?? 10;
            // 生成查询条件
            $where = array();
            if (!empty($post['nickname'])) {
                $where[] = ['nickname|name|mobile', 'like', '%' . $post['nickname'] . '%'];
            }
            $where[] = ['isdel','=',1];
            // 生成查询数据
            $count = $this->model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $this->model->where($where)->order("id asc")->limit((int)$limit)->page((int)$page)->select();
            $arr = [
                1 => '管理员',
                2 => '业务员',
                3 => '司机'
            ];
            foreach ($list as $row) {
                $row['type_text'] = $arr[$row['type']] ?? '普通用户';
            }
            // TODO..
            return $this->success('查询成功', "", $list, $count);
        }

        return view('/system/user/index', []);
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = request()->post();
            $post = request_validate_rules($post, get_class($this->model));
            if (empty($post) || !is_array($post)) {
                return $this->error($post);
            }
            // 禁止重复注册
            $whereMobile[] = ['mobile', '=', $post['mobile']];
            $whereMobile[] = ['isdel', '=',1];
            if ($this->model->whereOr([$whereMobile])->find()) {
                return $this->error('该用户ID或手机号已经存在！');
            }
            // 生成密码
            $post['password'] = md5(md5($post['pwd']));
            if ($this->model->create($post)) {
                return $this->success('注册成功！');
            }
            return $this->error('注册失败！');
        }
    }

    /**
     * 编辑会员
     */
    public function edit()
    {

        if (request()->isPost()) {

            $post = \request()->post();

            // 查询数据
            $data = $this->model->find($post['id']);
            if (!empty($post['mobile']) && $data['mobile'] != $post['mobile']) {
                $whereMobile[] = ['mobile', '=', $post['mobile']];
                $whereMobile[] = ['isdel', '=',1];
                if ($this->model->where($whereMobile)->find()) {
                    return $this->error('该用户手机号已经存在！');
                }
            }
            // 为空则去掉密码
            if (empty($post['pwd'])) {
                unset($post['pwd']);
            } else {
                $post['password'] = md5(md5($post['pwd']));
            }

            if ($this->model->update($post)) {
                return $this->success('更新成功！');
            }

            return $this->error('更新失败！');
        }
    }

    
	/**
	 * 删除轮播图
	 */
	public function del() 
	{
		$id = input('id');
		if (!empty($id) && is_numeric($id)) {
			if ($this->model::update(['isdel' => 2,'id' => $id])) {
				return $this->success('删除成功！');
			}
		}
		return $this->error('删除失败，请检查您的参数！');
	}

}
